var vue = new Vue({
	el: "#containt",
	data: {
		reDataIndex: 0, //重置到了2
		width: 0, //整个宽
		height: 0,
		connect_address: "00:20:08:18:B1:C1", //连接的地址
		isshowScanView: false, //是否显示扫描
		isscan: false, //是否开始扫描
		scanData: [], //扫描的数据
		isscanData: true, // 是否开始读取数据
		canInterver: "", //计时器
		readCard: 800, //扫描间隔
		statusIndex: 0, //连接状态
		Hometime: "", //首页时间
		inOutWareChar: "", //出入库的柱状图表格
		circleChar: "", //出入库的圆状图
		powerchar: "", //功率的饼图
		pinlvchar: "", //频率的饼图
		chinamap: "", //中国地图
		outSize: 1220, //出库数量
		inSize: 235, //入库数量
		wareCards: [], //仓库所有数据
		warelistview: "", //产品列表doc 为了获取宽高
		warelistScrollHeight: 0, //list需要移动的距离
		warelistMove: 0, //list当前移动的距离
		warelistHeight: 0, //list容器高度
		iswarelistscroll: true, //list是否滚动
		warelistmoveDiv: 1, //list每次移动的距离
		BlueToothPower: 0, //平台功率
		isInwareHome: true, //是入库还是出库
		isshowAddProduct: false, //是否显示添加产品
		isshowImageSelect: false, //是否显示所有选择图片
		allImageUrl: [], //所有的图片地址
		addproductObj: { //添加产品的初始数据
			"card": "", //E28068940000401893F2942A 测试卡号
			"img": "",
			"name": "",
			"status": 0, // 0 未入库 1入库 2出库
		},
		createCardData: [], //创建出入库view的数组
		isshowSetPower:false,//是否显示功率
		isshowsetPinlv:false,//是否显示频率
		setPowPinlvX:0, //功率和频率的显示x坐标
		setPowPinlvY:0,//功率和频率的显示y坐标
		isReadPause:false,//是否暂停读卡操作	
		
	},
	created() {
		this.width = window.innerWidth;
		this.height = window.innerHeight;
		this.initImageData();
		// this.initWareData();
		this.getwareData();
		if (this.wareCards.length == 0) {
			this.initWareData();
		}
		//开始计时
		// this.onstart_time();
		window.setTimeout(() => {
			//初始化出入库表格
			//出入库数据
			this.inOutWareChar = echarts.init(document.getElementById('in_out_ware'));
			this.circleChar = echarts.init(document.getElementById("circle_char"));
			this.powerchar = echarts.init(document.getElementById("gonglv_char"));
			this.pinlvchar = echarts.init(document.getElementById("pinlv_char"));
			// this.chinamap = echarts.init(document.getElementById("chinamap"));
			// this.chinamap.on('click',this.onClick_chinaMap);
			this.on_init_intoutWareData();
			this.on_init_intoutwareCircleData();
			this.on_init_powerCharData();
			this.on_init_pinlvCharData();
			// this.on_init_chinaecharData();

			//初始化高度和其他
			this.initWareListHeight();
			this.onListDataMove();
			//去开始连接
			this.onClick_onConnect();
		}, 1000);


	},
	watch: {
		statusIndex(n) {
			if (n == 7) {
				this.isscanData = true;
				//开始发送获取功率
				this.onGetPower();
				//开始发送查卡代码
			 	this.startReadCard();
			} else if (n == 9) {
				this.isscanData = false;
				//停止发送查卡
				this.stopReadCard();
			} else {
				this.isscanData = false;
			}
		},
		outSize(n) {
			charsview.refrechar();
		},
		inSize(n) {
			charsview.refrechar();
		},
		readCard(n) {
			charsview.refrechar();
		},
		BlueToothPower(n) {
			charsview.refrechar();
			this.onSetPower(n);
		}


	},
	methods: {
		//重置
		onClick_restartData() {
			this.reDataIndex += 1;
			if (this.reDataIndex == 2) {

				this.initWareData();
				this.initWareListHeight();
				this.reDataIndex = 0;
			}
		},
		//初始化图片数据
		initImageData() {
			for (var i = 1; i <= 4; i++) {
				this.allImageUrl.push("../static/pro/00" + i + ".png");
			}
		},
		//初始化仓库数据
		initWareData() {
			//添加初始化数据
			Ware.init();
			this.getwareData();
		},
		getwareData() {
			this.wareCards = Ware.getCards();
			//计算出入库的个数
			this.inSize = Ware.getInWareSize();
			this.outSize = Ware.getOutWareSize();

		},
		//重新获取仓库数据
		regetWareData() {
			this.getwareData();
		},
		//初始化列表高度和需要滚动的高度
		initWareListHeight() {
			this.warelistview = document.getElementsByClassName("right_view_con")[0];
			this.warelistHeight = this.warelistview.clientHeight;
			this.warelistScrollHeight = this.warelistview.scrollHeight;
		},
		//接收其他的数据来了
		onCardOther(data) {
			console.log("onCardOther data: ", data);
			//判断是否为
			if (data == "BB01B6000100B8") {
				Base.toast("设置功率成功!");
			}
		},
		//扫描的卡号
		onCardNum(card) {
			console.log("我的卡号", card);
			if (this.isshowAddProduct) { //是否处于打开产品中
				this.addproductObj.card = card;
				return;
			}
			if(this.isReadPause){
				return;
			}
			if (this.isInwareHome) { //入库
				this.onInWareDoc(card);
			} else { //出库
				this.onOutWareDoc(card);
			}

		},

		//接收停止扫描
		scan_stop() {
			this.isscan = false;
		},

		//接收当前蓝牙状态
		connect_status(status) {
			this.statusIndex = status;
		},

		//开始读卡
		startReadCard() {
			this.canInterver = window.setInterval(() => {
				try {
					window.frid.onReadCard();
				} catch (e) {}
			}, this.readCard);
		},
		//停止读卡
		stopReadCard() {
			if (this.canInterver != undefined) {
				window.clearInterval(this.canInterver)
			}
		},
		//入库还是出库
		onClickisInOrOut() {
			this.isInwareHome = !this.isInwareHome;
		},
		//时间显示
		onstart_time() {
			window.setInterval(() => {
				let d = new Date();
				let curr_year = d.getFullYear();
				let curr_month = d.getMonth() + 1;
				let curr_date = d.getDate();
				let curr_hour = d.getHours();
				let curr_min = d.getMinutes();
				let curr_mis = d.getSeconds();
				String(curr_month).length < 2 ? (curr_month = "0" + curr_month) : curr_month;
				String(curr_date).length < 2 ? (curr_date = "0" + curr_date) : curr_date;
				String(curr_hour).length < 2 ? (curr_hour = "0" + curr_hour) : curr_hour;
				String(curr_min).length < 2 ? (curr_min = "0" + curr_min) : curr_min;
				String(curr_mis).length < 2 ? (curr_mis = "0" + curr_mis) : curr_mis;
				this.Hometime = curr_hour + ":" + curr_min + ":" + curr_mis;
			}, 1000);
		},
		//初始化出入库数据
		on_init_intoutWareData() {
			charsview.showInOutData();
		},
		//初始化出入库饼图
		on_init_intoutwareCircleData() {
			charsview.showCirleChars();
		},
		//初始化功率的饼图
		on_init_powerCharData() {
			charsview.showPowerChar();
		},
		//初始化频率的饼图
		on_init_pinlvCharData() {
			charsview.showPinlvChar();
		},
		//初始化中国地图数据
		on_init_chinaecharData() {
			charsview.showChinaMap();
		},

		//数据列表自动滚动
		onListDataMove() {
			if ((this.warelistHeight == this.warelistScrollHeight) || !this.iswarelistscroll) {
				return
			}
			this.warelistMove += this.warelistmoveDiv;
			if (this.warelistMove >= (this.warelistScrollHeight - this.warelistHeight)) {
				this.warelistMove = 0;
			}
			this.warelistview.scrollTop = this.warelistMove;
			setTimeout(() => {
				this.onListDataMove();
			}, 80);
		},
		//开始扫描蓝牙
		onScanStartBlue() {

			this.isshowScanView = true; //打开扫描页
			try {
				if (this.isscanData) {
					this.onDisConnect();
				}
				if (this.isscan) {
					window.frid.onStopScan();
				}
				try {
					this.isscan = true;
					this.scanData = [];
					window.frid.onStartScan();
				} catch (e) {}
			} catch (e) {
				this.isscan = false;
				alert(e);
			}


		},
		//关闭扫描
		onClickCloseScan() {
			try {

				window.frid.onStopScan(); //停止扫描
			} catch (e) {}
			setTimeout(() => {
				this.isshowScanView = false; //关闭页面
			}, 750)

		},
		//扫描数据
		scan_data(name, address) {
			this.createTagOthen(name, address);
		},

		//随机生成
		testrand() {
			//随机生成一个
			this.createTagOthen("namedew", this.connect_address);
		},

		// 创建
		createTagOthen(name, address) {
			console.log(name, " 蓝牙地址 ", address);
			//随机x y 点坐标  - 减去自身的宽
			let startx = 70; //开始随机的点
			let endx = this.width * 0.6 - 70 // 
			let starty = 70; //开始随机的y点
			let endy = this.height * 0.7 - 70; //结束随机的y点
			let randX = this.getrand(startx, endx);
			let randY = this.getrand(starty, endy);


			if (name == "no") {
				return;
			}
			this.scanData.push({
				name,
				address,
				randX,
				randY
			});
		},
		// 随机计算中间值
		getrand(start, end) {
			return Math.floor(Math.random() * (end - start + 1) + start);
		},
		//扫描的item点击
		onClick_gotoConnect(address) {
			this.connect_address = address;
			setTimeout(() => {
				this.isshowScanView = false; //关闭页面
			}, 750)
			this.onClick_onConnect();
		},
		//去连接
		onClick_onConnect() {
			try { //停止扫描
				window.frid.onStopScan();
			} catch (e) {}
			try {
				//去连接
				window.setTimeout(() => {
					console.log("......去连接蓝牙...", this.connect_address);
					window.frid.onConnect(this.connect_address);
				}, 100);
			} catch (e) {
				alert(e);
			}

		},
		//重新连接
		onClick_ReConnect() {
			console.log("当前index :", this.statusIndex)
			if (this.statusIndex == 9) {
				try {
					window.frid.onConnect(this.connect_address);
				} catch (e) {
					alert(e);
				}
			}
		},
		//断开连接
		onDisConnect() {
			try {
				window.frid.onDisConnect();
				this.stopReadCard();
			} catch (e) {}
		},
		//获取功率
		onGetPower() {
			try {
			 
				//获取平台功率
				window.frid.onGetPower();
			} catch (e) {
				console.log("获取功率失败: ", e);
			}

		},
		//发送功率
		onSetPower(num) {
			try {
				// let num = Math.floor(Math.random() * 6);
				// console.log("num ", num, " ");
				console.log("js发送功率 ", num);
				window.frid.onSetPower(num);
			} catch (e) {}
		},
		//接收当前功率
		onPower(power) {
			this.BlueToothPower = power;
			if (power == -1) { //最大错误功率(未知进制值)
				this.onSetPower(5); //设置最大功率
				this.BlueToothPower = 5;
			}
		},
		//取消添加产品
		onClick_cancleAddProduct() {
			this.addproductObj.name = "";
			this.addproductObj.card = "";
			this.addproductObj.img = "";
			this.isshowImageSelect = false;
			this.isshowAddProduct = false;
		},
		//添加产品
		onClick_sureAddProduct() {
			if (this.addproductObj.card == "") {
				Base.toast("请扫描产品卡号!");
				return;
			}
			if (this.addproductObj.name == "") {
				Base.toast("请输入产品名称!");
				return;
			}
			if (this.addproductObj.img == "") {
				Base.toast("请选择产品图片!");
				return;
			}
			let isok = Ware.addCard(this.addproductObj);
			if (isok) {
				Base.toast("产品添加成功");
				//关闭添加页面
				this.onClick_cancleAddProduct();
				//重新获得仓库数据列表
				this.regetWareData();
				//重新计算滚动的高宽
				window.setTimeout(() => {
					this.initWareListHeight();
				}, 100);
				//重新获得仓库数据

			} else {
				Base.toast("该产品已存在,请重新选择卡号");
			}

		},
		//显示关闭添加产品页面
		onClick_isshowPorduct() {
			this.isshowImageSelect = false;
			this.isshowAddProduct = !this.isshowAddProduct;
		},
		//是否打开选择图片
		onClick_SelectImage() {
			this.isshowImageSelect = !this.isshowImageSelect;
		},
		//选择了图片
		onClick_selectImage(url) {
			this.addproductObj.img = url;
			this.isshowImageSelect = false;
		},
		//入库
		onInWareDoc(card) {
			let cards = Ware.getProduct(card);
			if (cards == null) {
				Base.toast("产品无法入库,产品不存在");
				return;
			}
			if (cards.status == 1) {
				Base.toast(cards.name + "已经入库");
				return;
			}
			Ware.InWareHome(card);
			//创建页面
			this.onCreateInoutView(cards);
			//重新获得仓库数据列表
			this.regetWareData();
		},
		//出库
		onOutWareDoc(card) {
			let cards = Ware.getProduct(card);
			if (cards == null) {
				Base.toast("产品无法出库,产品不存在");
				return;
			}
			if (cards.status == 2) {
				Base.toast(cards.name + "已经出库");
				return;
			}

			Ware.OutWareHome(card);
			//创建页面
			this.onCreateInoutView(cards);
			//重新获得仓库数据列表
			this.regetWareData();
		},
		onCreateInoutView(cards) {
			this.createCardData.push(cards);
			window.setTimeout(() => {
				this.createCardData.splice(0, 1);
			}, 4000);

		},
		//地图
		onClick_chinaMap(eve){
			console.log(eve);
		},
		//显示频率
		onClick_setpinlv(eve){
			console.log(eve);
			//显示设置频率页面
			this.setPowPinlvX = eve.pageX;
			this.setPowPinlvY = eve.pageY-eve.offsetY - 50;
			this.isshowsetPinlv = !this.isshowsetPinlv;
			//关闭设置功率页面
			this.isshowSetPower = false;
			  
			
		},
		//显示设置功率
		onClick_setPower(eve){
			this.setPowPinlvX = eve.pageX;
			this.setPowPinlvY = eve.pageY-eve.offsetY - 50;
			this.isshowSetPower = !this.isshowSetPower;
			this.isshowsetPinlv= false;
		},
		//是否暂停出入库
		onClick_PaseRead(){
			this.isReadPause =!this.isReadPause;
		}
		




	}
});
var ani;
window.onload = () => {
	ani = new ani();
	ani.init();
}
